// components/sort/sort.js
const componentBehavior = require('../../js/common/ComponentBehavior')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    sortList:{
      type:Array,
      value: [
        { name: '综合', sortcolligate: 'i-down', dataname: 'sortorder' },
        { name: '销量', sortcolligate: '', dataname: 'sells' },
        { name: '价格', sortcolligate: '', dataname: 'minprice' }
      ]
    },
    hasFilter:{
      type:Boolean,
      value:false
    },
    height: {
      type: String,
      value: '80rpx'
    },
  },

  /**
   * 组件的初始数据
   */
  data: { 
    sortList: [
      { name: '综合', sortcolligate: 'i-down', dataname: 'sortorder' },
      { name: '销量', sortcolligate: '', dataname: 'sells' },
      { name: '价格', sortcolligate: '', dataname: 'minprice' }
      ]
  },

  behaviors: [componentBehavior],

  /**
   * 组件的方法列表
   */
  methods: {
    bindSort(e){ 
      var e=e.currentTarget.dataset;
      var sortList=[];
      for (var i = 0; i < this.data.sortList.length;i++){
        var sort = this.data.sortList[i];
        var dataname = sort.dataname; 
        if (dataname==e.name){
          sort.sortcolligate= e.sort == "i-up" ? "i-down" : "i-up"
        }else{
          sort.sortcolligate="";
        }
        sortList.push(sort);
      }
      this.setData({
        sortList: sortList
      })
      var datas={
        sortField: e.name,
        sortDirect: e.sort == "i-up" ? "DESC" :"ASC"
      }
      this.triggerEvent("bindSort", datas);
    },
    onFilterEvent(e){
      this.triggerEvent("onFilterEvent", e);
    },
    reset () {
      let sortList = this.data.sortList
      for (let i in sortList) {
        sortList[i].sortcolligate = ''
      }
      sortList[0].sortcolligate = 'i-down'
      this.setData({
        sortList
      })
    },
    clear () {
      let sortList = this.data.sortList
      for (let i in sortList) {
        sortList[i].sortcolligate = ''
      }
      this.setData({
        sortList
      })
    }
  }
})
